package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000135658168";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCiQ2jKcd4yIp8d9WolT6fbWiv7fMLUF2y+e1Yl8N/bM4Ur3X6Id6yLXv6U/fAA1EP+qmjflLAp05YJ6foNFFSLQ/494Sne7OzsxnUnmqj9rrKzOw99R1xCvC3JBhuj/y8++nR/isu2aeIwUZ+egu9R9gGxqO5aW9twSO0oRfsplvBY4g/8apYIPqI9CUaVoTAis1/5USYKPH1XmW/VGAMKnIfkCGVBEmV2uFz96RvwcSIgs6uFazC/PuEDcU97Djrf6LIY+dz5sPUIw9eMfr8rKM6xjCpfGtvoO4287IdbjE7yle8tqT066Bd4GFZXrgIsN5bfdjaylhRfvypWy6VZAgMBAAECggEANnmPCrgjso0xAivMEO6nGdsx/OIrvmyG04QwrdgqLzsisFFSE5qp0Lh5m6eNFZotoaewQa93XPNt4bItgoqav456ImgfWHoOKIeP+VXQ+inWUJm5X4O/k5/9dwmX6fzX7brDtbXxIjJ1/2bHwb2PfuKqOuzN6IQCLbYt+GFWKuW4pOLXoJPFG0+XWptShZtXs6HYlwIEzOThhaw2iHhNf4nacOq33HPeT1mEks2JJWeKWQlCegrgQZBekkk//FCI57UJNJ6JTiZKsIbzcguI0BeXv96Jf643AhS/joie1DHHu4EmOxHrPxeLYn0HCSGhEj7tyNqoti4nBaUDTBYMKQKBgQDyvxnNl971fQjdITXkwqUGK3D0OHKwZP4Y6971xBX+2XgqSvqGTjtGR9/Qbm5EkYkE+dV8+BDT7echIIJIH7VJoLBTEyZh5Jz5Yqxu9p4WLjrMZcGNzB2aODDnL9UFewah4JoQ6WXgAbn2aF699vwIoUITNTIwANhxCSCNEenwRwKBgQCrH2KDpnpjJLHjPejHKCgzMWULhPyS7rxryok15hxEGEvtB2nH37EWuLntA2+gHYgUNY5kgGYWZVA1U1sqAweHD3JCAiEK6PYv1dLah7HChSynwaFoHqO5/ke9eyXrVHj4nW8zUXSxtQpbTrDCB79IgF7z/q+0TntHj7mqxsktXwKBgFhD8If3TxUIb+6oIsaZmBPUO/AxjlJhhptdOOc4c58FSFJojQvE6ny1qcZKQUIhgScUCPK16LiG6uH6xiSfkvXAvhyW+//gdsufe3+loPoDuVGVNnnu3Fy1TeLiW5zwjQrTdnHrzCVVu17DuJ9ubVpd+H6vex5FUG5ZmUJyXxQFAoGBAJUwgqIGlXM0fcaBph1j/FBNgyvc4Jt1el57sM6F9STFDgTzzpyMgIcvvCFQOpScypPb8W3bp6Vk5N3o83UYOQreQ54a3xX07xXbVL5jSW4uzzTekssE03eHoqIm6HNYLTOLhnR2UsfP79yVKXOwb2tVR/Nvm14WglJsktG6B5D/AoGABXrs1iseJjWDqRcIxIHAFxaugd3TdHxOWV4lBW7hDWBVSp1RPc/3kJzsXrcUKVAcfSYenE85A9DuD1hYdW0iTx9a1z7YoFoHq1bVD9ilmc3dR1tb1+ioTWNAhGovd0QHs3PbDaI/xTgF18Up3kGssxF4OxYFo+yxyVapAdYH0tw=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn8thIColjgOm3Wit3fuxwle2jdBkmVBozP+b+sJvCRlmzNdd2RG+tXqlk3vSeeksGMc7h5CNPlUF3YcPo2aO2FnrIhwzNIne72evm2Hapj+h8pSqh/bmYCWobEiuNhnVrgrp8b4EopveO5MW8nhWkIKP05+asFYPHefCYVmpI3zjeb4l9+yXgzLKuPQLhLI/2cNaExQx9r+q6BEtrt1gNl//sv3I3QBOahQpD/qjTZQWZY/OgMPKspVlW9svwA2y3hLx5KOB7K2Yw6JRP5hNwSGrlC/dTsSeBs/kINeQ0vAJz6/WNwNUSPje1oHPMISqG6hdFkU0xJbpxMSXEo9U+QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://7e30a036.r26.cpolar.top/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 同步通知，支付成功，一般跳转到成功页
    // private String return_url = "http://3fe13617.r26.cpolar.top/return_url.jsp";
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    // 支付超时时间，这里设置为一分钟，因为为了方便测试，order 服务设置的订单超时未支付时间是 1 分钟，延迟队列自动解锁订单库存的时间是 2 分钟
    // 项目上线后，订单超时未支付时间是 30 分钟，所以这里应该配置为 30 分钟
    private String timeout = "1m";
    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 业务请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        // 会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        // System.out.println("支付宝的响应：" + result);
        return result;
    }
}
